package com.best.oasis.settlement.dao.sysLog;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.best.oasis.settlement.common.entity.sysLog.SysLog;
import com.best.oasis.settlement.util.exception.DAOException;

@Repository
public class SysLogDaoImpl implements SysLogDao {

    protected static final String POSTFIX_INSERT = "insert";
    
    protected static final String POSTFIX_SPLIT  = ".";

    private static final Logger   logger         = Logger.getLogger(SysLogDaoImpl.class);

    @Autowired
	protected SqlSessionTemplate sqlSessionTemplate;
    
    public void create(SysLog entity) {
        try {
        	logger.info(getStatementPrefix() + POSTFIX_INSERT);
        	sqlSessionTemplate.insert(getStatementPrefix() + POSTFIX_INSERT, entity);
        } catch (Exception e) {
            logger.error(getStatementPrefix() + "error occured when method create invocked.");
            logger.error(e);
            throw new DAOException(e);
        }
    }

    public void log(String type, String description) {
        SysLog sysLog = new SysLog(type, description);
        logger.info(sysLog);
        //数据库里面去掉对description和userPrivileges的记录，不然数据量太大
        sysLog.setUserPrivileges(null);
        sysLog.setZDescription(null);
        this.create(sysLog);
    }

    protected String getStatementPrefix() {
        return SysLog.class.getSimpleName() + POSTFIX_SPLIT;
    }
}
